﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class CalendarGroupDetailDA : ICalendarGroupDetailDA
    {
        private string CALENDAR_GROUP_ID = "@CALENDAR_GROUP_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string END_TIME = "@END_TIME";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string LINE_ID = "@LINE_ID";
        private string RATE_ID = "@RATE_ID";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_CALENDAR_GROUP_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@CALENDAR_GROUP_DETAIL WHERE CALENDAR_GROUP_ID = @CALENDAR_GROUP_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_CALENDAR_DETAIL = " DELETE FROM @_@CALENDAR_GROUP_DETAIL WHERE CALENDAR_GROUP_ID = @CALENDAR_GROUP_ID";
        private string SQL_DELETE_CALENDAR_GROUP_DETAIL = " DELETE FROM @_@CALENDAR_GROUP_DETAIL WHERE CALENDAR_GROUP_ID = @CALENDAR_GROUP_ID AND LINE_ID = @LINE_ID ";
        private string SQL_INSERT_CALENDAR_GROUP_DETAIL = " INSERT INTO @_@CALENDAR_GROUP_DETAIL ( WH_ID, CALENDAR_GROUP_ID, LINE_ID, START_TIME, END_TIME, RATE_ID, REMARK, IS_ACTIVE, UPDATED_DATE, UPDATED_BY, CREATED_DATE, CREATED_BY) VALUES ( @WH_ID, @CALENDAR_GROUP_ID, @LINE_ID, @START_TIME, @END_TIME, @RATE_ID, @REMARK, @IS_ACTIVE, GETDATE(), @UPDATED_BY, GETDATE(), @CREATED_BY )  ";
        private string SQL_SELECT_ALL_CALENDAR_GROUP_DETAIL = " SELECT  ROWID, WH_ID, CALENDAR_GROUP_ID, LINE_ID, START_TIME, END_TIME, RATE_ID, REMARK, IS_ACTIVE, UPDATED_DATE, UPDATED_BY, CREATED_DATE, CREATED_BY  FROM  @_@CALENDAR_GROUP_DETAIL ";
        private string SQL_UPDATE_CALENDAR_GROUP_DETAIL = " UPDATE @_@CALENDAR_GROUP_DETAIL SET WH_ID = @WH_ID, START_TIME = @START_TIME, END_TIME = @END_TIME, RATE_ID = @RATE_ID, REMARK = @REMARK, IS_ACTIVE = @IS_ACTIVE, UPDATED_DATE = GETDATE(), UPDATED_BY = @UPDATED_BY WHERE CALENDAR_GROUP_ID = @CALENDAR_GROUP_ID AND LINE_ID = @LINE_ID  ";
        private string START_TIME = "@START_TIME";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckCalendarGroupDetailIDUnique(string calendarGroupDetailID, string LineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendarGroupDetailID), new SqlParameter("@LINE_ID", LineID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_CALENDAR_GROUP_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckCalendarGroupDetailIDUnique(DataBase dataBase, DbTransaction tran, string calendarGroupDetailID, string LineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendarGroupDetailID), new SqlParameter("@LINE_ID", LineID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_CALENDAR_GROUP_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteCalendarGroupDetail(string calendarGroupDetailID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendarGroupDetailID) };
                result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CALENDAR_DETAIL.Replace("@_@", whLoginID), paras);
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteCalendarGroupDetail(string calendarGroupDetailID, List<string> LineID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < LineID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendarGroupDetailID), new SqlParameter("@LINE_ID", LineID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteCalendarGroupDetail(DataBase dataBase, DbTransaction tran, string calendarGroupDetailID, List<string> LineID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < LineID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendarGroupDetailID), new SqlParameter("@LINE_ID", LineID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllCalendarGroupDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllCalendarGroupDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public List<CalendarGroupDetailInfo> GetCalendarGroupDetailByByRateID(string rateID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID) + " WHERE RATE_ID = @RATE_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RATE_ID", rateID) };
            List<CalendarGroupDetailInfo> calendarGroupDetailList = new List<CalendarGroupDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    CalendarGroupDetailInfo calendarGroupDetailInfo = new CalendarGroupDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CALENDAR_GROUP_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, (reader["START_TIME"].ToString() != "") ? DateTime.Parse(reader["START_TIME"].ToString()) : (CS00002 = new DateTime()), (reader["END_TIME"].ToString() != "") ? DateTime.Parse(reader["END_TIME"].ToString()) : (CS00002 = new DateTime()), reader["RATE_ID"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["CREATED_BY"].ToString());
                    calendarGroupDetailList.Add(calendarGroupDetailInfo);
                }
            }
            return calendarGroupDetailList;
        }

        public List<CalendarGroupDetailInfo> GetCalendarGroupDetailByByRateID(DataBase dataBase, DbTransaction tran, string rateID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID) + " WHERE RATE_ID = @RATE_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@RATE_ID", rateID) };
            List<CalendarGroupDetailInfo> calendarGroupDetailList = new List<CalendarGroupDetailInfo>();
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                CalendarGroupDetailInfo calendarGroupDetailInfo = new CalendarGroupDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CALENDAR_GROUP_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, (reader["START_TIME"].ToString() != "") ? DateTime.Parse(reader["START_TIME"].ToString()) : (CS00002 = new DateTime()), (reader["END_TIME"].ToString() != "") ? DateTime.Parse(reader["END_TIME"].ToString()) : (CS00002 = new DateTime()), reader["RATE_ID"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["CREATED_BY"].ToString());
                calendarGroupDetailList.Add(calendarGroupDetailInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return calendarGroupDetailList;
        }

        public List<CalendarGroupDetailInfo> GetCalendarGroupDetailByID(string calendargroupdetailID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID) + " WHERE CALENDAR_GROUP_ID = @CALENDAR_GROUP_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendargroupdetailID) };
            List<CalendarGroupDetailInfo> calendarGroupDetailList = new List<CalendarGroupDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    CalendarGroupDetailInfo calendarGroupDetailInfo = new CalendarGroupDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CALENDAR_GROUP_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, (reader["START_TIME"].ToString() != "") ? DateTime.Parse(reader["START_TIME"].ToString()) : (CS00002 = new DateTime()), (reader["END_TIME"].ToString() != "") ? DateTime.Parse(reader["END_TIME"].ToString()) : (CS00002 = new DateTime()), reader["RATE_ID"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["CREATED_BY"].ToString());
                    calendarGroupDetailList.Add(calendarGroupDetailInfo);
                }
            }
            return calendarGroupDetailList;
        }

        public CalendarGroupDetailInfo GetCalendarGroupDetailByID(string calendargroupdetailID, string LineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID) + " WHERE CALENDAR_GROUP_ID = @CALENDAR_GROUP_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendargroupdetailID), new SqlParameter("@LINE_ID", LineID) };
            CalendarGroupDetailInfo calendarGroupDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003;
                    calendarGroupDetailInfo = new CalendarGroupDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CALENDAR_GROUP_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, (reader["START_TIME"].ToString() != "") ? DateTime.Parse(reader["START_TIME"].ToString()) : (CS00003 = new DateTime()), (reader["END_TIME"].ToString() != "") ? DateTime.Parse(reader["END_TIME"].ToString()) : (CS00003 = new DateTime()), reader["RATE_ID"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["CREATED_BY"].ToString());
                }
            }
            return calendarGroupDetailInfo;
        }

        public List<CalendarGroupDetailInfo> GetCalendarGroupDetailByID(DataBase dataBase, DbTransaction tran, string calendargroupdetailID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID) + " WHERE CALENDAR_GROUP_ID = @CALENDAR_GROUP_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendargroupdetailID) };
            List<CalendarGroupDetailInfo> calendarGroupDetailListInfo = new List<CalendarGroupDetailInfo>();
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                CalendarGroupDetailInfo calendarGroupDetailInfo = new CalendarGroupDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CALENDAR_GROUP_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, (reader["START_TIME"].ToString() != "") ? DateTime.Parse(reader["START_TIME"].ToString()) : (CS00002 = new DateTime()), (reader["END_TIME"].ToString() != "") ? DateTime.Parse(reader["END_TIME"].ToString()) : (CS00002 = new DateTime()), reader["RATE_ID"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["CREATED_BY"].ToString());
                calendarGroupDetailListInfo.Add(calendarGroupDetailInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return calendarGroupDetailListInfo;
        }

        public CalendarGroupDetailInfo GetCalendarGroupDetailByID(DataBase dataBase, DbTransaction tran, string calendargroupdetailID, string LineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID) + " WHERE CALENDAR_GROUP_ID = @CALENDAR_GROUP_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendargroupdetailID), new SqlParameter("@LINE_ID", LineID) };
            CalendarGroupDetailInfo calendarGroupDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                DateTime CS00003;
                calendarGroupDetailInfo = new CalendarGroupDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CALENDAR_GROUP_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, (reader["START_TIME"].ToString() != "") ? DateTime.Parse(reader["START_TIME"].ToString()) : (CS00003 = new DateTime()), (reader["END_TIME"].ToString() != "") ? DateTime.Parse(reader["END_TIME"].ToString()) : (CS00003 = new DateTime()), reader["RATE_ID"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["CREATED_BY"].ToString());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return calendarGroupDetailInfo;
        }

        public DataSet GetCalendarGroupDetailByQueryList(List<string> sqlWhere, CalendarGroupDetailQueryEntity calendargroupdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!calendargroupdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, calendargroupdetailQuery.CurrentPage, calendargroupdetailQuery.PageSize, calendargroupdetailQuery.SortField, calendargroupdetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetCalendarGroupDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, CalendarGroupDetailQueryEntity calendargroupdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, calendargroupdetailQuery.CurrentPage, calendargroupdetailQuery.PageSize, calendargroupdetailQuery.SortField, calendargroupdetailQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertCalendarGroupDetail(CalendarGroupDetailInfo calendarGroupDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                calendarGroupDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CalendarGroupDetail_Parameters(calendarGroupDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertCalendarGroupDetail(DataBase dataBase, DbTransaction tran, CalendarGroupDetailInfo calendarGroupDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                calendarGroupDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CalendarGroupDetail_Parameters(calendarGroupDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_CalendarGroupDetail_Parameters(CalendarGroupDetailInfo calendarGroupDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.CALENDAR_GROUP_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.START_TIME, SqlDbType.DateTime, 8), new SqlParameter(this.END_TIME, SqlDbType.DateTime, 8), new SqlParameter(this.RATE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.REMARK, SqlDbType.VarChar, 100), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(calendarGroupDetailInfo.WhID))
            {
                paramArray[0].Value = calendarGroupDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = calendarGroupDetailInfo.CalendarGroupID;
            paramArray[2].Value = calendarGroupDetailInfo.LineID;
            paramArray[3].Value = calendarGroupDetailInfo.StartTime;
            paramArray[4].Value = calendarGroupDetailInfo.EndTime;
            paramArray[5].Value = calendarGroupDetailInfo.RateID;
            if (!string.IsNullOrEmpty(calendarGroupDetailInfo.Remark))
            {
                paramArray[6].Value = calendarGroupDetailInfo.Remark;
            }
            else
            {
                paramArray[6].Value = DBNull.Value;
            }
            paramArray[7].Value = calendarGroupDetailInfo.IsActive;
            paramArray[8].Value = calendarGroupDetailInfo.UpdatedBy;
            paramArray[9].Value = calendarGroupDetailInfo.CreatedBy;
            return paramArray;
        }

        public int UpdateCalendarGroupDetail(CalendarGroupDetailInfo calendarGroupDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                calendarGroupDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CalendarGroupDetail_Parameters(calendarGroupDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateCalendarGroupDetail(DataBase dataBase, DbTransaction tran, CalendarGroupDetailInfo calendarGroupDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                calendarGroupDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CalendarGroupDetail_Parameters(calendarGroupDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_CALENDAR_GROUP_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

